<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="新闻名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入新闻名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="新闻标签" prop="tag">
        <el-input
          v-model="queryParams.tag"
          placeholder="请输入新闻标签"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="一级分类" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择" @change="secodType"  @keyup.enter.native="handleQuery" clearable>
          <el-option
            v-for="item in typeOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类" prop="typeTwo">
        <el-select v-model="queryParams.typeTwo" placeholder="请选择" @change="secodType"  @keyup.enter.native="handleQuery" clearable>
          <el-option
            v-for="item in typeTwoOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['content:press:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['content:press:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['content:press:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['content:press:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pressList" @selection-change="handleSelectionChange" border="border">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" :index="createIndex" />
      <el-table-column label="公司logo" align="center" prop="pcUrl">
       <!-- 图片的显示 -->
                 <template   slot-scope="scope">            
                    <img :src="scope.row.pcUrl"  min-width="70px" height="70px" 
                           @click="previewPic(scope.row.pcUrl)" />
                 </template>     
      </el-table-column>
      <el-table-column label="公司名称" align="center" prop="name">
        <template slot-scope="scope">
          <span @click="viewName(scope.row)">{{scope.row.name}}</span>
        </template>
      </el-table-column>
      <el-table-column label="会内职务" align="center" prop="tag" />
       <el-table-column label="浏览人数" align="center" prop="browseNumber" />
      <el-table-column label="是否允许评论" align="center" prop="commentType">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.commentType==0" type="warning">不允许</el-tag>
          <el-tag v-if="scope.row.commentType==1" type="success">允许</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否置顶" align="center" prop="overhead">
           <template slot-scope="scope">
          <el-tag v-if="scope.row.overhead==0" type="warning">否</el-tag>
          <el-tag v-if="scope.row.overhead==1" type="success">是</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" align="center" prop="releaseTime" width="180">
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['content:press:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['content:press:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 图片预览   放在表格外面，这是个弹出框-->
      <el-dialog :visible.sync="dialogVisible" :modal="false" title="图片预览" width="50%">
        <img :src="previewpic" alt=""  width="100%" />
        <span slot="footer" class="dialog-footer">
          </span>
       </el-dialog>
  <!-- 图片预览 -->

  <!--查看具体内容-->
 <el-dialog :visible.sync="viewOpen" :modal="false" :title="title" width="50%">
   <el-form ref="form" :model="form">
<div style="width: 100%" class="previewWrapper">
	<div class="previewContainer">
		<div class="card" style="padding:10px 10px 10px 10px;text-align:left;display: none" >
		</div>
		<div class="card" style="padding:10px 10px 10px 10px;text-align:center;display: none">
			<strong>{{form.name}}</strong>
		</div>
		<div class="card" style="text-align:center;display: none">
			<div><span class="badge badge-dark">{{form.tag}}</span></div>
		</div>
		<div class="card" style="padding:10px 10px 10px 10px;display: none">
		</div>
		<div style="max-height: 667px" class="contentContainer">
			<div style="margin-bottom: 10px">
				<strong style="font-size: 18px;">{{form.name}}</strong>
			</div>
			<span v-html="form.content"></span>
		</div>
	</div>
</div>
   </el-form>
 </el-dialog>
    <!-- 添加或修改新闻对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-row :gutter="20">
         <el-col :span="12" :xs="24" >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文章链接">
          <el-col :span="18" :xs="24">
          <el-input type="text"  v-model="input" clearable placeholder="请输入文章链接" ref="conten_val"></el-input >
          </el-col>
          <el-col :span="6" :xs="24">
          <el-button type="primary" icon="el-icon-chat-dot-round" round @click="getContent(form)">导入</el-button>
          </el-col>
        </el-form-item>
       <el-form-item label="新闻名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入新闻名称" />
        </el-form-item>
        <el-form-item label="新闻标签" prop="tag">
          <el-input v-model="form.tag" placeholder="请输入新闻标签" />
        </el-form-item>
        <el-form-item label="一级分类" prop="type">
          <el-select v-model="form.oneType" placeholder="请选择" @change="secodType" multiple clearable >
          <el-option
            v-for="item in typeOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="二级分类" prop="typeTwo">
          <el-select v-model="form.twoType" placeholder="请选择" clearable multiple>
          <el-option
            v-for="item in typeTwoOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="封面图片" prop="pcUrl">
          <el-upload
            class="avatar-uploader"
            ref="upload"
            :action="upload.url"
            :headers="upload.headers"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            >
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="外链地址" prop="linkUrl">
          <el-input v-model="form.linkUrl" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否置顶" prop="overhead">
         <el-select v-model="form.overhead" placeholder="请选择">
          <el-option
            v-for="item in overheadOptions"
            :key="item.value"
            :label="item.lable"
            :value="item.value">
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="文章分组" prop="articleType">
          <el-select v-model="form.articleType" placeholder="请选择">
          <el-option
            v-for="item in articleTypeOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="发布时间" prop="releaseTime">
          <el-date-picker clearable
            v-model="form.releaseTime"
            type="date"
            placeholder="请选择发布时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="浏览人数" prop="browseNumber">
          <el-input v-model="form.browseNumber" placeholder="请输入浏览人数" />
        </el-form-item>
      </el-form>
      </el-col>
      <el-col :span="12" :xs="24">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" class="contentForm">
       <el-form-item label="内容" >
          <div class="divList">
            <div class="scrollbar_wrap">
            <editor v-model="form.content" :min-height="710"/>
            </div>
          </div>
        </el-form-item>
      </el-form>
      </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPress, getPress, delPress, addPress, updatePress,getScoedtype,getCardType,addContent } from "@/api/content/press";
import { getToken } from "@/utils/auth";
export default {
  name: "Press",
  data() {
    return {
      //图片放大
      dialogVisible: false,
      previewpic: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 新闻表格数据
      pressList: [],
      border:true,
      // 弹出层标题
      title: "",
      //一级分类
      typeOptions:[],
      //二级分类
      typeTwoOptions:[],
      //会员分组
      articleTypeOptions:[],
      //是否置顶
      overheadOptions:[
        {
        value:0,
        lable:'否'
        },
        {
          value:1,
          lable:'是'
        }
      ],
      //具体内容弹出层
      viewOpen:false,
      // 是否显示弹出层
      open: false,
      input:'',
      //上传参数
      upload:{
          // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/content/press/importData"
      },
       imageUrl: '',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        asId: null,
        name: null,
        tag: null,
        type: null,
        typeTwo: null,
        typeName: null,
        creator: null,
        content: null,
        pcUrl: null,
        linkUrl: null,
        commentType: null,
        overhead: null,
        flag: 2,
        releaseTime: null,
        status: null,
        browseNumber: null,
        pitchOn: null,
        editId: null,
        childUserId: null,
        reprintIds: null,
        reproduceIds: null,
        sortId: null,
        mediaId: null,
        mediaCode: null,
        townId: null,
        articleType: null,
      },
      // 查询参数
      typeQueryParams: {
        flag:2
      },

      // 表单参数
      form: {},
      // 表单校验
      rules: {
        oneType: [
          { required: true, message: "新闻一级分类ID不能为空", trigger: "change" }
        ],
        name: [
          { required: true, message: "新闻名称不能为空", trigger: "blur" }
        ],
        tag: [
          { required: true, message: "新闻标签不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    //创建序号
    createIndex(index){
      return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index +1;
    },
    /** 查询新闻列表 */
    getList() {
      this.loading = true;
      listPress(this.queryParams).then(res => {
        this.pressList = res.data.rows;
        this.total = res.data.total;
        this.typeOptions=res.typeList;
        this.loading = false;
      });
    },
    getContent(form){
      this.loading = true;
      const url=this.$refs.conten_val.value;
      const id=form.id;
    addContent(url,id).then(res=>{
      this.form=res.data;
      this.imageUrl=res.data.pcUrl;
      this.form.flag=2;
      this.loading = false;
    })
    },
    //查看具体内容
    viewName(row){
      console.info(row);
      this.form=row;
      this.viewOpen=true;
      this.title="查看内容";
    },
    //图片放大
      previewPic(url) {
           this.previewpic = url;
           this.dialogVisible = true;
        },
      
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        asId: null,
        name: null,
        tag: null,
        type: null,
        typeTwo: null,
        typeName: null,
        creator: null,
        content: null,
        pcUrl: null,
        linkUrl: null,
        commentType: null,
        overhead: null,
        flag: null,
        releaseTime: null,
        createTime: null,
        updateTime: null,
        status: null,
        browseNumber: null,
        pitchOn: null,
        editId: null,
        childUserId: null,
        reprintIds: null,
        reproduceIds: null,
        sortId: null,
        mediaId: null,
        mediaCode: null,
        townId: null,
        articleType: null,
        oneType:[],
        twoType:[]
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    //上传图片地址
      handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
        if(res.code==200){
          this.form.pcUrl=res.data;
        }else{
          console.info(res.msg)
        }
      },
    
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getCardType(this.typeQueryParams).then(res=>{
        this.articleTypeOptions=res.data.assDictList;
        this.typeOptions=res.data.typeList;
        this.open = true;
        this.title = "添加新闻";
    })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPress(id).then(res => {
        this.form = res.data;
        this. imageUrl=res.data.pcUrl;
        this.typeOptions=res.allList;
        this.typeTwoOptions=res.secondTypes;
        this.articleTypeOptions=res.assDictList;
        this.open = true;
        this.title = "修改新闻";
      });
    },
       //获取二级分类数据
    secodType(val){
    console.info(val)
    if(val.length==0){
      this.typeTwoOptions=[];
    }else{
         getScoedtype(val).then(res=>{
        this.typeTwoOptions=res.data
      })
    }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePress(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.flag==2;
            addPress(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除新闻编号为"' + ids + '"的数据项？').then(function() {
        return delPress(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('content/press/export', {
        ...this.queryParams
      }, `press_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style >
.divList{
  height:710px;
}
.scrollbar_wrap{
  height: 100%;
  overflow-x: hidden;
}
.scrollbar_wrap::-webkit-scrollbar {
  display: none;
}

 .avatar-uploader .el-upload {
    border: 1px solid #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    border: 1px solid #d9d9d9;
    border-radius: 6px;
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
 .previewWrapper{
		display: flex;
		justify-content: center;
	}
	.previewContainer{
		width: 400px !important;
		height: 667px !important;
		background-image: url("https://shequncloud.oss-cn-shenzhen.aliyuncs.com/material/phoneKK.png");
		background-repeat: no-repeat;
		background-size: 100% 100%;
		padding: 85px 35px 86px 35px;
		box-sizing: border-box;
		position: relative;
	}
	.contentContainer{
		height: 100%;
		overflow-y: auto;
		overflow-x: hidden;
	}
  .contentContainer::-webkit-scrollbar {
  display: none;
}
</style>
